//
//  NSString+TYExtendsion.m
//  TYToolKit
//
//  Created by dev_tianyu on 16/8/6.
//  Copyright © 2016年 dev_tianyu. All rights reserved.
//

#import "NSString+TYExtendsion.h"
#import <Foundation/Foundation.h>

@implementation NSString (TYExtendsion)

#pragma mark - 字符串获取

/**
 *  获取唯一字符串 CFUUID
 *
 *  @return 唯一字符串
 */
+ (NSString *)uniqueString {
    
    CFUUIDRef puuid = CFUUIDCreate(nil);
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *strUUID = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);

    return strUUID;

}

/**
 *  根据时间戳获取唯一字符串
 *
 *  @return 根据时间戳获取的唯一字符串
 */
+ (NSString *)uniqueStringFromTimeStamp {
    
    NSDate *datenow =[NSDate date];//现在时间
    NSLog(@"%@",datenow);
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:datenow];
    NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[localeDate timeIntervalSince1970]];
    
    return timeSp;
}

#pragma mark - 字符串属性设置方法

/**
 *  设置指定字符串颜色
 *
 *  @param string 指定的字符串
 *  @param color  颜色
 *
 *  @return 设置颜色后的字符串
 */
- (NSString *) setRangeOfString:(NSString *)string withColor:(UIColor *)color {
    
    return [self setRangeOfString:string WithColor:color WithFont:nil];
    
}

/**
 *  设置指定字符串的颜色和字体
 *
 *  @param string 指定的字符串
 *  @param color  颜色
 *  @param font   字体
 *
 *  @return 添加属性后的字符串
 */
- (NSString *)setRangeOfString:(NSString *)string WithColor:(UIColor *)color WithFont:(UIFont *)font {
    
    NSRange range = [self rangeOfString:string];
    
    if (range.location != NSNotFound) {
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self];
        
        [attrStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        if (font != nil) {
            [attrStr addAttribute:NSFontAttributeName value:font range:range];
        }
    }
    
    return self;
}

#pragma mark - 字符串检测相关方法

/**
 *  判断字符串是否全部为数字
 *
 *  @return
 */
- (BOOL) isAllNum {
    unichar c;
    for (int i = 0 ; i < self.length; i++) {
        c = [self characterAtIndex:i];
        if (!isdigit(c)) {
            return NO;
        }
    }
    
    return YES;
}

/**
 *  判断是当前字符串否包含子串
 *
 *  @param subString 子串
 *
 *  @return
 */
- (BOOL)isHaveString:(NSString *)subString {
    NSRange _range = [self rangeOfString:subString];
    if (_range.location != NSNotFound) {
        return YES;
    }else {
        return NO;
    }
}

/**
 *  判断是否包含中文
 *
 *  @param string
 *
 *  @return
 */
- (BOOL)isHaveChineseInString{
    for(NSInteger i = 0; i < [self length]; i++){
        int a = [self characterAtIndex:i];
        if (a > 0x4e00 && a < 0x9fff) {
            return YES;
        }
    }
    return NO;
}

@end
